package com.ddl.controller;


import com.ddl.common.exception.ExceptionUtil;
import com.ddl.common.exception.ResultModel;
import com.ddl.service.SmsCodeService;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author: liyuxiang
 * @create: 2018-11-04
 */
@RestController
@RequestMapping(value = "/sms")
public class SmsCodeController {

    private final static Logger logger = LoggerFactory.getLogger(SmsCodeController.class);

    @Autowired
    SmsCodeService smsCodeService;

    /**
     * 根据type,发送短信验证码
     *
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/sendSmsCode")
    public ResultModel sendSmsCode(@RequestBody Map<String, Object> map) {
        try {
            String phone = map.get("phone").toString();
            String type = map.get("type").toString();
            smsCodeService.sendSmsCode(phone, type);
            return ResultModel.getSuccessInstance();
        } catch (Exception e) {
            logger.error("sendSmsCode fail, map:{}", new Gson().toJson(map), e);
            return ExceptionUtil.getMessageException(e);
        }
    }
}
